package com.jcroke.PC32.basetransform;

import java.util.LinkedHashMap;

import com.jcroke.DACGraph.Pnode;
import com.jcroke.PC32.datum.Datum;
import com.jcroke.PC32.exception.ValueStoreException;

/**
 * 
 * @author jcroke
 *
 */
public final class ValueStore extends Transformer {

	public void prepare(Pnode pnode) {
		this._pnode = pnode;

		// Check if the value at memory location is not null.
		// this would indicate that it has been previously initialized.
		System.out.println("[Prepare] ValueStore runnning prepare on: " + _pnode.getNodeName());
		if (_umem.read(_pnode.getNodeName(), _pnode.getOutputSignature().get(0)
				.getSourcePin()) == null) {

			String valueType = _pnode.getOutputSignature().get(0)
					.getSourceTypeClass();
			
			String value = Pnode.substrIRI(_pnode.getLogic()
					.getSourceRDFValue());
			
			Datum valueDatum = null;
			
			try {
				valueDatum = (Datum) Class.forName(valueType)
						.getConstructor(String.class).newInstance(value);
			} catch (Exception e) {
				throw new ValueStoreException(
						"ValueStore transformer could not instantiate value datum class: "
								+ valueType + " for value: " + value, e);
			}
			_umem.write(_pnode.getNodeName(), _pnode.getOutputSignature()
					.get(0).getSourcePin(), valueDatum);
		}
	}

	public LinkedHashMap<String, Datum> receive() {
		return null;
	}

	public void execute() {
	}
}
